#include "StdAfx.h"
#include "TestNumber.h"

#include "GeptTestNumber.h"
#include "HakkaTestNumber.h"
#include "FlptTestNumber.h"

TestNumber* TestNumber::Create( UINT uTestTypeId,const CString& strTestNumber )
{
	TestNumber* pTestNumber = NULL;
	
	switch(uTestTypeId)
	{
		case GeptTestNumber::TypeId: {
			pTestNumber = new GeptTestNumber(strTestNumber);
		}break;
		case HakkaTestNumber::TypeId: {
			pTestNumber = new HakkaTestNumber(strTestNumber);
		}break;
		case FlptTestNumber::TypeId: {
			pTestNumber = new FlptTestNumber(strTestNumber);
		}break;			
	}

	if(FALSE == pTestNumber->IsValid(strTestNumber))
		SAFE_DELETE(pTestNumber);

	return pTestNumber;
}

TestNumber::TestNumber(const CString& strTestNumber,const CString& strFormatMask, const CString& strValidFormat, UINT nDigits)
:m_strTestNumber(strTestNumber)
,m_strFormatMask(strFormatMask)
,m_strValidFormat(strValidFormat)
,m_nDigits(nDigits)
{

}
TestNumber::~TestNumber(void)
{
}

CString TestNumber::GetString( int uDisplaySize/*=-1*/ ) const
{
	if(0 > uDisplaySize || uDisplaySize > m_strTestNumber.GetLength())
		uDisplaySize = m_strTestNumber.GetLength();

	return m_strTestNumber.Mid(m_strTestNumber.GetLength() - uDisplaySize);
}

void TestNumber::SetValue( TestNumber& rhs )
{
	m_strTestNumber = rhs.m_strTestNumber;
}

bool TestNumber::SetValue( __int64 iTestNumber )
{
	// 123-1234-12345
	CString strPrefix = m_strTestNumber.Mid(0,m_strTestNumber.GetLength()-m_nDigits);

	CString strPostfixFmt;
	strPostfixFmt.Format(_T("%%0%dI64d"),m_nDigits);
	CString strPostfix;
	strPostfix.Format(strPostfixFmt,iTestNumber);

	CString strTestNumber(strPrefix + strPostfix);
	
	bool bResult = false;

	if(bResult = IsValid(strTestNumber))
	{
		m_strTestNumber = strTestNumber;
	}

	return bResult;
}
bool TestNumber::IsValid(const CString& strTestNumber)const
{
	return strTestNumber == _T("ALWAYS_PASS");
}

__int64 TestNumber::GetValue() const
{
	CString strValue = m_strTestNumber.Mid(m_strTestNumber.GetLength()-m_nDigits);
	return _ttoi64(strValue);
}

bool TestNumber::operator-=(__int64 i)
{
	return operator+=(-1*i);
}

bool TestNumber::operator==(TestNumber& rhs)
{
	return GetString() == rhs.GetString();
}

bool TestNumber::operator<(const TestNumber& rhs)const
{
	return GetValue()<rhs.GetValue();
}

const CString& TestNumber::GetFormatMask()
{
	return m_strFormatMask;
}

const CString& TestNumber::GetValidFormat()
{
	return m_strValidFormat;
}
__int64 operator-(const TestNumber& lhs,const TestNumber& rhs)
{
	return lhs.GetValue() - rhs.GetValue();
}
